package com.bmadmin.controller.zcgl;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.LayUiDataTablePOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.domain.ZcFlwhDomain;
import com.bmadmin.domain.ZcwhDomain;
import com.bmadmin.service.ZcwhService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/zcwh")
public class ZcwhController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());
	
    @Autowired
    ZcwhService zcwhService;
    
    /**
     * 获取资产列表
     */
	@GetMapping("/getZcList")
    public Object getZcList(HttpSession session,ZcwhDomain zcwhDomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("zc_bm", zcwhDomain.getZc_bm());
		map.put("zc_mc", zcwhDomain.getZc_mc());
		map.put("zcfl_dm", zcwhDomain.getZcfl_dm());
		logger.info(zcwhDomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<ZcwhDomain> list = zcwhService.getZcList(map);
		
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 获取资产下拉
     */
    @GetMapping("/getZcSel")
    public LayUiDataTablePOJO getZcSel(String zcfl_dm) {
        LayUiDataTablePOJO result = zcwhService.getZcSel(zcfl_dm);
        logger.info(result);
        return result;
    }
	
    @PostMapping("/insertZcwh")
    public NormalResult insertZcwh(HttpSession session,ZcwhDomain zcwh) {
    	String log = "资产新增>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcwh.getZc_mc())){
        		result.setCode("9999");
                result.setMsg("资产名称为空！");
                logger.info(log+"资产名称为空！");
                return result;
        	}
//        	if(!BMUtil.isNotNull(zcwh.getZc_bm())){
//        		result.setCode("9999");
//                result.setMsg("资产编码为空！");
//                logger.info(log+"资产编码为空！");
//                return result;
//        	}
        	if(!BMUtil.isNotNull(zcwh.getZcfl_dm())){
        		result.setCode("9999");
                result.setMsg("资产分类为空！");
                logger.info(log+"资产分类为空！");
                return result;
        	}
//        	Map map = new HashMap();
//    		map.put("zc_bm", zcwh.getZc_bm());
//    		List<ZcwhDomain> list = zcwhService.getZcList(map);
//    		if(list.size()>0){
//    			result.setCode("9999");
//                result.setMsg("资产编码已存在！");
//                logger.info(log+"资产编码已存在！");
//                return result;
//    		}
        	Map map1 = new HashMap();
    		map1.put("mc", zcwh.getZc_mc());
    		List<ZcwhDomain> list1 = zcwhService.getZcList(map1);
    		if(list1.size()>0){
    			result.setCode("9999");
                result.setMsg("资产名称已存在！");
                logger.info(log+"资产名称已存在！");
                return result;
    		}
        	int retResult = zcwhService.insertZcwh(zcwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    @PostMapping("/deleteZc")
    public NormalResult deleteZc(String zc_id) {
    	String log = "资产删除>>>>>>>>>>>";
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(zc_id)) {
            result.setCode("9999");
            result.setMsg("传入ID为空!");
            return result;
        }

        int retResult = zcwhService.deleteZc(zc_id);
        if(retResult>0){
    		result.setCode("0000");
            result.setMsg("删除成功！");
            logger.info(log+"删除成功！");
    	}else{
    		result.setCode("9999");
            result.setMsg("删除失败！");
            logger.info(log+"删除失败！");
    	}
    	return result;
    }
    
    @PostMapping("/updateZc")
    public NormalResult updateZc(HttpSession session,ZcwhDomain zcwh) {
    	String log = "资产修改>>>>>>>>>>>";
    	logger.info(log+"参数【"+zcwh+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(zcwh.getZc_id())){
        		result.setCode("9999");
                result.setMsg("资产ID为空！");
                logger.info(log+"资产ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcwh.getZc_mc())){
        		result.setCode("9999");
                result.setMsg("资产名称为空！");
                logger.info(log+"资产名称为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcwh.getZc_bm())){
        		result.setCode("9999");
                result.setMsg("资产编码为空！");
                logger.info(log+"资产编码为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(zcwh.getZcfl_dm())){
        		result.setCode("9999");
                result.setMsg("资产分类为空！");
                logger.info(log+"资产分类为空！");
                return result;
        	}
        	Map map = new HashMap();
    		map.put("zc_bm", zcwh.getZc_bm());
    		List<ZcwhDomain> list = zcwhService.getZcList(map);
    		if(list.size()>0){
    			ZcwhDomain rs = (ZcwhDomain)list.get(0);
    			if(!zcwh.getZc_id().equals(rs.getZc_id())){
        			result.setCode("9999");
                    result.setMsg("资产编码已存在！");
                    logger.info(log+"资产编码已存在！");
                    return result;
    			}

    		}
        	Map map1 = new HashMap();
    		map1.put("mc", zcwh.getZc_mc());
    		List<ZcwhDomain> list1 = zcwhService.getZcList(map1);
    		if(list1.size()>0){
    			ZcwhDomain rs1 = (ZcwhDomain)list1.get(0);
    			if(!zcwh.getZc_id().equals(rs1.getZc_id())){
	    			result.setCode("9999");
	                result.setMsg("资产名称已存在！");
	                logger.info(log+"资产名称已存在！");
	                return result;
    			}
    		}
        	int retResult = zcwhService.updateZcwh(zcwh);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("修改成功！");
                logger.info(log+"修改成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("修改失败！");
                logger.info(log+"修改失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("资产修改异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取资产分类信息
     */
    @GetMapping("/getZcInfo")
    public NormalResult getZcInfo(HttpSession session,String zc_bm){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	NormalResult result = new NormalResult();
    	if (BMUtil.isNullOrEmpty(zc_bm)) {
            result.setCode("9999");
            result.setMsg("传入资产编码为空!");
            return result;
        }
    	Map map = new HashMap();
    	map.put("zc_bm",zc_bm);
    	List<ZcwhDomain> list = zcwhService.getZcList(map);
    	ZcwhDomain zcwhDomain = list.get(0);
       
        result.setCode("0000");
        result.setMsg("success");
        result.setData(zcwhDomain);
        return result;
    }
    
    /**
     *资产查询导出
     */
    @PostMapping("/exportFile")
    public Object exportFile(HttpSession session,ZcwhDomain zcwhDomain) {
		String log = "资产查询导出>>>>>>>>>>>";
		NormalResult result = new NormalResult();
		Map map = new HashMap();
		map.put("zc_bm", zcwhDomain.getZc_bm());
		map.put("zc_mc", zcwhDomain.getZc_mc());
		map.put("zcfl_dm", zcwhDomain.getZcfl_dm());
		logger.info(zcwhDomain);//列表条件输出
		
		List<ZcwhDomain> list = zcwhService.getZcList(map);
		
        if(list!=null&&list.size()>0){
        	// 声明一个工作薄
    		HSSFWorkbook workbook = new HSSFWorkbook();
    		// 用于格式化单元格的数据
    		HSSFDataFormat format = workbook.createDataFormat();

    		HSSFCellStyle stylecol = workbook.createCellStyle();
    		stylecol.setDataFormat(format.getFormat("@"));
    		// 设置单元格类型
    		HSSFCellStyle cellStyle = workbook.createCellStyle();
    		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平布局：居中
    		cellStyle.setWrapText(true);

    		HSSFSheet sheet = workbook.createSheet();
    		// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
    		HSSFRow row = sheet.createRow((int) 0);
    		sheet.autoSizeColumn(1, true);

    		HSSFFont font = workbook.createFont();
    		/*
    		 * font.setFontName("黑体"); font.setFontHeightInPoints((short)
    		 * 16);//设置字体大小 HSSFFont font2 = workbook.createFont();
    		 * font2.setFontName("仿宋_GB2312");
    		 * font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
    		 * font2.setFontHeightInPoints((short) 12);
    		 */
    		font.setColor(HSSFColor.RED.index);
    		
            // 创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

            HSSFCell cell = row.createCell(0);
            cell.setCellValue("序号");
    		cell.setCellStyle(style);
    		
    		cell = row.createCell(1);
    		cell.setCellValue("资产编码");
    		cell.setCellStyle(style);
    		
    		cell = row.createCell(2);
    		cell.setCellValue("资产名称");
    		cell.setCellStyle(style);

    		cell = row.createCell(3);
    		cell.setCellValue("资产分类");
    		cell.setCellStyle(style);

    		cell = row.createCell(4);
    		cell.setCellValue("规格型号");
    		cell.setCellStyle(style);

    		cell = row.createCell(5);
    		cell.setCellValue("计量单位");
    		cell.setCellStyle(style);

    		cell = row.createCell(6);
    		cell.setCellValue("供应商");
    		cell.setCellStyle(style);
    		
    		cell = row.createCell(7);
    		cell.setCellValue("库存数量");
    		cell.setCellStyle(style);
    		
    		cell = row.createCell(8);
    		cell.setCellValue("库存阈值");
    		cell.setCellStyle(style);
    		
    		for (int i = 0; i < list.size(); i++) {
    			row = sheet.createRow((int) i + 1);
    			// ，创建单元格，并设置值
    			cell = row.createCell(0);
    			cell.setCellValue(i + 1);
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(0, stylecol);

    			cell = row.createCell(1);
    			cell.setCellValue(list.get(i).getZc_bm());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(1, stylecol);

    			cell = row.createCell(2);
    			cell.setCellValue(list.get(i).getZc_mc());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(2, stylecol);

    			cell = row.createCell(3);
    			cell.setCellValue(list.get(i).getZcfl_mc());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(3, stylecol);

    			cell = row.createCell(4);
    			cell.setCellValue(list.get(i).getZc_ggxh());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(4, stylecol);

    			cell = row.createCell(5);
    			cell.setCellValue(list.get(i).getZc_jldw());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(5, stylecol);

    			cell = row.createCell(6);
    			cell.setCellValue(list.get(i).getZc_gys());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(6, stylecol);
    			
    			cell = row.createCell(7);
    			cell.setCellValue(list.get(i).getZc_sl());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(7, stylecol);
    			
    			cell = row.createCell(8);
    			cell.setCellValue(list.get(i).getZc_fz());
    			cell.setCellStyle(style);
    			sheet.setDefaultColumnStyle(8, stylecol);
    		}
    		FileOutputStream fos = null;
            String uuid = BMUtil.getUUID();
    		try {
                String filename = "D:\\soft\\nginx\\html\\hzwgs\\exportfile\\"+uuid+".xls";

                fos = new FileOutputStream(filename);

                workbook.write(fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    		result.setCode("0000");
            result.setMsg("Excel文件获取成功！");
            result.setObject(uuid);
            logger.info(log+"Excel文件获取成功！");
        }else{
        	result.setCode("9999");
            result.setMsg("无导出数据");
            logger.info(log+"无导出数据！");
        }
        
		return result;
	}
}
